#

| Chapter 5 Introducing the ex Editor |

## 5.3 Saving and Exiting Files

You have learned the vi command `ZZ` to quit and write (save) your file. But you will frequently want to exit a file using ex commands, because these commands give you greater control. We've already mentioned some of these commands in passing. Now let's take a more formal look.

`:w`

  Writes (saves) the buffer to the file but does not exit. You can (and should) use `:w` throughout your editing session to protect your edits against system failure or a major editing error.

`:q`

  Quits the file (and returns to the UNIX prompt).

`:wq`

  Both writes and quits the file. Both writes and quits (exits) the file. It's the same as `:wq`.

vi protects existing files and your edits in the buffer. For example, if you want to write your buffer to an existing file, vi gives you a warning. Likewise, if you have invoked vi on a file, made edits, and want to quit without saving the edits, vi gives you an error message such as:

>     No write since last change.

These warnings can prevent costly mistakes, but sometimes you want to proceed with the command anyway. An exclamation point (`!`) after your command overrides the warning:

>     :w!
>     :q!

`w!` can also be used to save edits in a file that was opened in read-only mode with `vi` `-R` or `view`.

`:q!` is an essential editing command that allows you to quit without affecting the original file, regardless of any changes you made in this session. The contents of the buffer are discarded.

### 5.3.1 Renaming the Buffer

You can also use `:w` to save the entire buffer (the copy of the file you are editing) under a new filename.

Suppose you have a file practice, containing 600 lines. You open the file and make extensive edits. You want to quit but save both the old version of practice and your new edits for comparison. To save the edited buffer in a file called practice.new, give the command:

>     :w practice.new

Your old version, in the file practice, remains unchanged (provided that you didn't previously use `:w`). You can now quit the old version by typing `:q`.

### 5.3.2 Saving Part of a File

While editing, you will sometimes want to save just part of your file as a separate, new file. For example, you might have entered formatting codes and text that you want to use as a header for several files.

You can combine ex line addressing with the write command, `w`, to save part of a file. For example, if you are in the file practice and want to save part of practice as the file newfile, you could enter:

`:230,$w` `newfile`

  Saves from line 230 to end of file in `newfile`.

`:.,600w` `newfile`

  Saves from the current line to line 600 in newfile.

### 5.3.3 Appending to a Saved File

You can use the UNIX redirect and append operator (`>>`) with `w` to append all or part of the contents of the buffer to an existing file. For example, if you entered:

>     :1,10w newfile

then:

>     :340,$w >>newfile

newfile would contain lines 1-10 and from line 340 to the end of the buffer.

5.2 Editing with ex |  | 5.4 Copying a File into Another File